package com.zlt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zlt.dao.CompanyDao;
import com.zlt.dao.Impl.CompanyDaoImpl;
import com.zlt.dao.Impl.InterviewDaoImpl;
import com.zlt.dao.Impl.JobDaoImpl;
import com.zlt.dao.Impl.UserDaoImpl;
import com.zlt.dao.InterviewDao;
import com.zlt.dao.JobDao;
import com.zlt.dao.UserDao;
import com.zlt.dto.InterviewDTO;
import com.zlt.entity.Company;
import com.zlt.entity.Interview;
import com.zlt.entity.User;
import com.zlt.entity.ViewInterview;
import com.zlt.service.InterviewService;
import com.zlt.util.ActiveUserUtil;
import com.zlt.util.JWTUtil;
import com.zlt.vo.ActiveUser;
import com.zlt.vo.InterviewVo;
import com.zlt.vo.Pager;
import com.zlt.vo.ViewInterviewVo;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class InterviewServiceImpl implements InterviewService {

    private InterviewDao interviewDao = new InterviewDaoImpl();

    private UserDao userDao = new UserDaoImpl();

    private CompanyDao companyDao =new CompanyDaoImpl();

    private JobDao jobDao = new JobDaoImpl();

    @Override
    public boolean publishInterview(InterviewDTO interviewDTO) {
        /*
        校验
         */


        Interview interview =new Interview();
        try {
            BeanUtils.copyProperties(interview,interviewDTO);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            throw new RuntimeException("当前未登录");
        }

        User user =userDao.selectUserByUsername(interviewDTO.getUname());

        Company company = jobDao.selectCidByUid(activeUser.getUid(),0);


        interview.setInitiatorId(activeUser.getUid());
        interview.setCid(company);
        interview.setUid(user);

        return interviewDao.insertInterview(interview) > 0;
    }

    @Override
    public Pager<InterviewVo> selectInterview(Pager<InterviewVo> pager, InterviewDTO interviewDTO) {
        return null;
    }

    @Override
    public List<ViewInterviewVo> showInfo(String token) {
        String username = JWTUtil.getUsername(token);
        List<ViewInterview> viewInterviews = interviewDao.getInterviewViewByUsername(username);
        List<ViewInterviewVo> viewInterviewVos = new ArrayList<>();

        for(ViewInterview viewInterview : viewInterviews){
            viewInterviewVos.add(
                    new ViewInterviewVo(
                            viewInterview.getId(),
                            viewInterview.getCompanyName(),
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(viewInterview.getSendTime()),
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(viewInterview.getValidTime()),
                            viewInterview.getState(),
                            viewInterview.getName(),
                            viewInterview.getNote(),
                            viewInterview.getInitiator(),
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(viewInterview.getInterviewTime()),
                            viewInterview.getAddress()
                    )
            );
        }
        return viewInterviewVos;
    }

    @Override
    public boolean AcceptOrReject(String username,String id,String state) {
        String s = null;
        if ("accept".equals(state)){
            s="4";
        }else if ("reject".equals(state)){
            s="5";
        }
        return interviewDao.setInterviewState(username,id,s);
    }

    @Override
    public Pager<InterviewVo> selectAllInterview(Pager<InterviewVo> pager, InterviewDTO interviewDTO) {

        ActiveUser activeUser = ActiveUserUtil.get();
        if (activeUser == null){
            throw new RuntimeException("当前未登录");
        }

        Company company = jobDao.selectCidByUid(activeUser.getUid(),0);

        //查询总条数
        Long count =interviewDao.selectInterviewCount();
        pager.setTotalCount(count);
        List<Interview> interviews = interviewDao.selectAllInterview(activeUser.getUid(),company.getCid(),pager);
        List<InterviewVo> interviewVoList = new ArrayList<>();
        interviews.forEach(i -> {
            InterviewVo interviewVo = new InterviewVo();
            interviewVoList.add(interviewVo);
            //通过id查询到发布者插入到返回前台的数据中
            String id = i.getInitiatorId();
            User initiator = userDao.selectUserByUid(id);

            User uid = userDao.selectUserByUid(i.getUid().getUid());
            System.out.println(uid.toString());
            //通过cid查询公司完整信息
            Company company1 = companyDao.selectCompanyInfoByUid(i.getInitiatorId());
            System.out.println(company1.toString());


            try {
                BeanUtils.copyProperties(interviewVo,i);
                interviewVo.setCid(company1);
                interviewVo.setUid(uid);
                interviewVo.setInitiator(initiator);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        });
        pager.setData(interviewVoList);
        System.out.println(JSONObject.toJSONString(pager));
        return pager;
    }

    @Override
    public boolean updateInterview(Integer id, String state) {
        System.out.println(interviewDao.updateInterview(id,state) > 0);
        return interviewDao.updateInterview(id,state) > 0;
    }
}
